﻿/*!!-- Map Builder --!!*/

var DIRECTIONS = {
NORTH:0,
EAST:1,
SOUTH:2,
WEST:3,
DIR_SIZE:4
}
function xy(a_x,a_y) {
	this.x = a_x;
	this.y = a_y;
}

function BuildMap(MapData,height,width) {
	var arrPos ={x:0,y:0,itt:function(h,w) {
		this.x += 1;
		if(this.x >= w) {
			this.x = 0;
			this.y += 1;
			if(this.y >= h) {
				return false;
			}
		}
		return true;
	},
	revitt:function(w) {
		this.x -= 1;
		if(this.x < 0) {
			this.x = width-1;
			this.y -= 1;
		}
	}};
	var x = 0;
	var y = 0;
	var MapITT = 0;
	//+x +y -x -y
	//NORTH, EAST, SOUTH, WEST
	var Mod = [new xy(0,-1),new xy(1,0),new xy(0,1),new xy(-1,0)];
	var SetStart = false;
	var SetEnd = false;
	var Continue = false;
	var tileSet = false;
	var index = 0;
	var type = "None";
	var toSet = [false,false,false,false];
	///!!!***--- Build all Paths ---***!!!///
	do{	
		x = arrPos.x;
		y = arrPos.y;
		
		tileSet = false;
		switch(MapData[MapITT]) {
		/*!!-- Modifiers --!!*/
		case 'x':case 'X':
			arrPos.revitt(width);
			SetStart = true;
			break;
			
		case 'z':case 'Z':	
			arrPos.revitt(width);
			SetEnd = true;
			break;
			
		/*!!-- Critical Components --!!*/		
		/*-- Horizontal Paths -- */
		
		//Horizontal
		case 'l':case 'L':
			index = indexHorizontal;
			type = "Horizontal";
			tileSet = true;
			break;
			
		/*-- Vertical Paths -- */

		//Vertical
		case 'k':case 'K':
			index = indexVertical;
			type = "Vertical";
			tileSet = true;
			break;
			
		/*-- Turns --*/
		
		//Top to left
		case 'f':case 'F':
			index = indexTopLeft;
			type = "Top Left";
			tileSet = true;
			break;
		//Top to right
		case 't':case 'T':
			index = indexTopRight;
			type = "Top Right";
			tileSet = true;
			break;
		//Bottom to right
		case 'h':case 'H':
			index = indexBottomRight;
			type = "Bottom Right";
			tileSet = true;
			break;
		
		//Bottom to left
		case 'g':case 'G':
			index = indexBottomLeft;
			type = "Bottom Left";
			tileSet = true;
			break;
		
		/*!!-- Secondary Components --!!*/
		
		/*-- T Paths -- */
		
		//Up T -
		case 'w':case 'w':
			index = indexTUp;
			type = "T Up";
			tileSet = true;
			break;
		//Down T -
		case 's':case 'S':
			index = indexTDown;
			type = "T Down";
			tileSet = true;
			break;
		//Left T ¦
		case 'a':case 'A':
			index = indexTLeft;
			type = "T Left";
			tileSet = true;
			break;s
		//Right T +
		case 'd':case 'D':
			index = indexTRight;
			type = "T Right";
			tileSet = true;
			break;
			
		/*!!-- Extra Components --!!*/
		}
		if(tileSet) {
			this.RawMapArray[x + (y * height)] = new PathTile(x,y,x * tileSizeX,y * tileSizeY, index,type,null);
		}
		if(SetStart == true && tileSet) {
			this.RawMapArray[x + (y * height)].type = "Start";
			this.AddStart(this.RawMapArray[x + (y * height)]);
			
			SetStart = false;
		}
		if(SetEnd == true && tileSet) {
			this.RawMapArray[x + (y * height)].type = "End";
			SetEnd = false;
		}
		MapITT += 1;
		
		
		Continue = arrPos.itt(height,width);
	}while(Continue);

	
	///!!!***--- Link all Paths ---***!!!///
	MapITT = 0;
	arrPos.x = 0;
	arrPos.y = 0;
	do {
			
		x = arrPos.x;
		y = arrPos.y;
		toSet = [false,false,false,false];
		tileSet = false;
		switch(MapData[MapITT]) {
		/*!!-- Modifiers --!!*/
		case 'x':case 'X':
			arrPos.revitt(width);
			SetStart = true;
			break;
			
		case 'z':case 'Z':
			arrPos.revitt(width);		
			SetEnd = true;
			break;
		/*!!-- Critical Components --!!*/
		
		/*-- Horizontal Paths --*/
		
		//Horizontal going right
		case 'l':case 'L':
			tileSet = true;
			toSet[DIRECTIONS.EAST] = true;
			toSet[DIRECTIONS.WEST] = true;
			
			break;
			
		/*-- Vertical Paths --*/
		
		//Vertical
		case 'k':case 'K':
			tileSet = true;
			toSet[DIRECTIONS.NORTH] = true;
			toSet[DIRECTIONS.SOUTH] = true;
			break;
		
		/*-- Turns --*/
		
		//Top to left
		case 'f':case 'F':
			tileSet = true;
			toSet[DIRECTIONS.WEST] = true;
			toSet[DIRECTIONS.NORTH] = true;
			
			break;
		
		//Top to right
		case 't':case 'T':
			tileSet = true;
			toSet[DIRECTIONS.EAST] = true;
			toSet[DIRECTIONS.NORTH] = true;
			
			
			break;
		//Bottom to right
		case 'h':case 'H':
			tileSet = true;
			toSet[DIRECTIONS.EAST] = true;
			toSet[DIRECTIONS.SOUTH] = true;			
			break;
			
		//Bottom to left
		case 'g':case 'G':
			tileSet = true;
			toSet[DIRECTIONS.WEST] = true;
			toSet[DIRECTIONS.SOUTH] = true;
			break;
			
		/*!!-- Secondary Components --!!*/
		
		/*-- T Paths --*/
		
		//Up T -
		case 'w':case 'w':
			tileSet = true;
			toSet[DIRECTIONS.WEST] = true;
			toSet[DIRECTIONS.NORTH] = true;
			toSet[DIRECTIONS.EAST] = true;
			break;
		//Down T -
		case 's':case 'S':
			tileSet = true;
			toSet[DIRECTIONS.WEST] = true;
			toSet[DIRECTIONS.SOUTH] = true;
			toSet[DIRECTIONS.EAST] = true;
			break;
		//Left T ¦
		case 'a':case 'A':
			tileSet = true;
			toSet[DIRECTIONS.WEST] = true;
			toSet[DIRECTIONS.SOUTH] = true;
			toSet[DIRECTIONS.NORTH] = true;
			break;
		//Right T +
		case 'd':case 'D':
			tileSet = true;
			toSet[DIRECTIONS.EAST] = true;
			toSet[DIRECTIONS.SOUTH] = true;
			toSet[DIRECTIONS.NORTH] = true;
			break;
			
		/*!!-- Extra Components --!!*/
		}
		if(tileSet) {
			if( toSet[DIRECTIONS.NORTH] == true && y+Mod[DIRECTIONS.NORTH].y >= 0 )
				this.RawMapArray[x + (y * height)].addNext(this.RawMapArray[x + ((y+Mod[DIRECTIONS.NORTH].y) * height)]);
			if( toSet[DIRECTIONS.SOUTH] == true && y+Mod[DIRECTIONS.SOUTH].y < height )
				this.RawMapArray[x + (y * height)].addNext(this.RawMapArray[x + ((y+Mod[DIRECTIONS.SOUTH].y) * height)]);
			if( toSet[DIRECTIONS.EAST] == true && x+Mod[DIRECTIONS.EAST].x < width )
				this.RawMapArray[x + (y * height)].addNext(this.RawMapArray[(x+Mod[DIRECTIONS.EAST].x) + (y * height)]);
			if( toSet[DIRECTIONS.WEST] == true && x+Mod[DIRECTIONS.WEST].x >= 0 )
				this.RawMapArray[x + (y * height)].addNext(this.RawMapArray[(x+Mod[DIRECTIONS.WEST].x) + (y * height)]);
		}
		if(SetStart) {
			SetStart = false;
		}
		if(SetEnd) {
			this.RawMapArray[x + (y * height)].next = this.RawMapArray[x + (y * height)];
			SetEnd = false;
		}
		MapITT += 1;
		Continue = arrPos.itt(height,width);
	}while(Continue);
}
